package clound

import (
	"learn-opengl/camera"
	"learn-opengl/object/block"
	"learn-opengl/opengl/shader"
	"math"
	"math/rand"
	"time"

	"github.com/go-gl/mathgl/mgl32"
)

// 生成云朵
// 水泥色
type Clound struct{
	Pos mgl32.Vec3
	Show []mgl32.Vec3
}

func NewClound(Pos mgl32.Vec3) *Clound{
	cl := &Clound{
		Pos: Pos,
	}
	cl.InitClound()
	return cl
}


func (c *Clound) InitClound() {
	rand.Seed(time.Now().Unix())
	n := 8 - rand.Int()%8
	mp := make(map[int]int)
	for i:=0;i<n;i++ {
		x := rand.Int()%100 - 50
		y := rand.Int()%100 - 50
		if mp[x*1000+y] == 1 {
			i--
			continue
		}
		mp[x*1000+y] = 1
		for i:=0;i<10;i++ {
			for j:=0;j<10;j++ {
				if rand.Int()%3 == 0 || (i>4&&i<7&&j>4&&j<7) {
					c.Show = append(c.Show, mgl32.Vec3{float32(x)+c.Pos[0]+float32(i),30,float32(y)+c.Pos[2]-float32(j)})
				}
			}
		}
	}


	
}

func (c *Clound) Draw(camera *camera.Camera,shader *shader.Shader) {
	for i:=0;i<len(c.Show);i++ {
		// 圆锥体优化
		cPos := camera.GetPos()
		front := camera.GetFront()
		p := c.Show[i].Sub(cPos)
		
		angle := math.Acos(float64(front.Normalize().Dot(p.Normalize())))
		if p.Len()>100{
			continue
		}
		if angle < math.Pi/4 {
			block.Blocks[16].Draw(shader,c.Show[i])
		}
	}
}

